import java.util.*;

/**This class represent a itemset in the algorithm.
 * A itemset contains items, which is a set of string, for the implementation of algorithm
 * this items are ordered by the lexical order.
 * */
public class Itemset implements Comparable{
	
	/**TreeSet for items
	 * */
	private SortedSet <String> items=new TreeSet<String>();
	/**the occurent of this itemset in all transaction
	 * */
	private int count;
	private double sup;
	
	public Itemset(){
		
	}
	
	public Itemset(String s, int i){
		items=new TreeSet<String>();
		items.add(s);
		count=i;
		
	}
	public Itemset(TreeSet<String> rowSet){
		items=rowSet;
	
	}
	public int size(){
		return items.size();
	}
	
	public Set<String> getItems(){
		return items;
	}
	public double getSup(){
		return sup;
	}
	public void setSup(double s){
		sup=s;
	}
	

	public void addCount(){
		count++;
	}
	public int getCount(){
		return count;
	}

	public void addItems(Set<String> iSet){
		
		items.addAll(iSet);
	}
	public void addItems(Set<String> iSet1, Set<String> iSet2){
		
	    items.addAll(iSet1);
	    items.addAll(iSet2);
		
	}
	
	/**
	 * We would order the item set by their lexical order
	 * */	
	public int compareTo(Object o){
		
		Itemset iSet=(Itemset) o;
		if(this.equals(iSet)){
			return 0;
		}
		
		Iterator<String> iT1=items.iterator();
		Iterator<String> iT2=iSet.getItems().iterator();
		
		while(iT1.hasNext()&&iT2.hasNext()){
			String T1=iT1.next();
			String T2=iT2.next();
			if(T1.compareTo(T2)!=0){
				return T1.compareTo(T2);
			}
		}
		if(!iT1.hasNext()){
		   return -1;
		}else{
			return 1;
		}
				
	}
	/**In the algorithm, we need to join two itemset depends on whether they have
	 * first size-1 item the same.
	 * */
	public boolean prefixEquals(Itemset iSet){
				
		int size=items.size();
		
		Iterator<String> iT1=items.iterator();
		Iterator<String> iT2=iSet.getItems().iterator();
		
		for(int i=0;i<size-1&&iT1.hasNext()&&iT2.hasNext();i++){
			
	//		System.out.println("inside equal");
			
	//		Thread.sleep(1500);
			
			String T1=iT1.next();
			String T2=iT2.next();
			
		//	System.out.println(T1+"\t"+T2);
			
			if(!T1.equals(T2)){
				return false;
			}
		}
		return true;
	}
	/**The itemset with the same itemset would be deemed as equal.
	 * */
	public boolean equals(Object o){
		
		Itemset iSet=(Itemset) o;
		
		if(iSet.size()!=items.size()){
			return false;
		}
		if(iSet.size()==items.size()){	
			
			Iterator <String> iT1=items.iterator();
			Iterator <String> iT2=iSet.getItems().iterator();
			
			while(iT1.hasNext()){
				String T1=iT1.next();
				String T2=iT2.next();
				if(!T1.equalsIgnoreCase(T2)){
					return false;
				}
			}     
			
		}			 
	  return true;
	}
	
	public String toString(){
		
		return items+"\t"+sup*100+"%\n";
	}
}
